package com.cs456.TeamBobcat;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Configuration extends Activity {
	protected Button _submit;
	protected EditText _username;
	protected EditText _serverAddress;
	protected EditText _serverPort;
	
	protected SharedPreferences _prefs;
	
	public static String USERNAME = "username";
	public static String SERVER_ADDRESS = "server_address";
	public static String SERVER_PORT = "server_port";
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configuration);
		_prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		initSubmit();
		initInputFields();
	}

	private void initInputFields() {
		_username = (EditText) findViewById(R.id.config_username);
		_serverAddress = (EditText) findViewById(R.id.config_server_address);
		_serverPort = (EditText) findViewById(R.id.config_portnum);
		
		_username.setText(_prefs.getString(USERNAME, ""));
		_serverAddress.setText(_prefs.getString(SERVER_ADDRESS, ""));
		_serverPort.setText(_prefs.getString(SERVER_PORT, ""));
		
	}

	private void initSubmit() {
		_submit = (Button) findViewById(R.id.config_submit);
		_submit.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Editor editor = _prefs.edit();
				editor.putString(USERNAME, 			_username.getText().toString());
				editor.putString(SERVER_ADDRESS, 	_serverAddress.getText().toString());
				editor.putString(SERVER_PORT, 		_serverPort.getText().toString());
				editor.commit();
				
				finish();
			}
		});
	}
}
